package com.sam.im.heiqun.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.sam.im.heiqun.app.App;
import com.sam.im.heiqun.callmain.IMMessage;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.AiteEntivity;
import com.sam.im.heiqun.entities.CircleNums;
import com.sam.im.heiqun.entities.FileMsgEntivity;
import com.sam.im.heiqun.entities.GetUUID;
import com.sam.im.heiqun.entities.GroupFriendEntivity;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.ImGroupEntivity;
import com.sam.im.heiqun.entities.ImMessage;
import com.sam.im.heiqun.entities.MessageEntivity;
import com.sam.im.heiqun.entities.MsgEntity;
import com.sam.im.heiqun.entities.ReadedEntity;
import com.sam.im.heiqun.entities.SearchUserEntity;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.entities.VoiceEntivity;
import com.sam.im.heiqun.entities.model.WithdrawEntity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.activities.AScallActivity;
import com.sam.im.heiqun.uis.activities.LoginActivity;
import com.sam.im.heiqun.uis.activities.PayActivity;
import com.sam.im.heiqun.uis.adapters.ChatGroupRecyclerAdapter;
import com.sam.im.heiqun.uis.adapters.ChatRecyclerAdapter;
import com.google.gson.Gson;
import com.sam.im.heiqun.uis.beans.NoticeBean;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.utils.EventBusType;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * 接受服务端数据进行分析
 *
 * @author Jeo
 */
public class ServiceDataAnalyse {

    private static String TAG = ServiceDataAnalyse.class.getSimpleName();
    private static App mApp = App.getInstance();

    public static void onMessage(Object... args) {
        String currentAct = getRunningActivityName(App.getInstance());
        String message = args[0].toString();
        Log.i(TAG, "onMessage: " + message);
        final ImMessage imMessage = IMMessageToJson.jsonToImMessage(message);
        Log.i(TAG, "onMessage: 22222222222222" + message);
        if (imMessage == null) {
            return;
        }

        //判断消息是否重复 情景是同一设备两个账号之间发消息
        List<ImMessage> imMessages = ImMessage.find(ImMessage.class, "msg_id=? and uniqueness=?", new String[]{imMessage.getMsgId(), imMessage.getFromid() + "_" + imMessage.getDestid()});
        List<ImMessage> im = ImMessage.find(ImMessage.class, "msg_id=? and uniqueness=?", new String[]{imMessage.getMsgId(), imMessage.getFromid() + "__" + imMessage.getDestid()});
        if (imMessages.size() > 0) {
            Log.i("info", "0001消息重复了");
            return;//消息重复了，此处抛弃掉,
        }
        if (im.size() > 0) {
            Log.i("info", "002消息重复了");
            return;
        }

        Integer messageType = imMessage.getMessageType();
        if(messageType == Constant.NOTICE_OF_APPLY_FRIEND){
            imMessage.setFromType(Constant.FROM_TYPE_SYS);
        }
        if (messageType == Constant.MESSAGE_TYPR_GROUP_SET_MANAGER
                || messageType == Constant.MESSAGE_TYPR_GROUP_CANCEL_MANAGER
                || messageType == Constant.MESSAGE_TYPR_ADD_GROUP
                || messageType == Constant.NOTICE_OF_GROUP_OUT
                | messageType == Constant.MESSAGE_GROUP_ADD_APPLY
                || messageType == Constant.MESSAGE_TYPR_BYE_GROUP) {
            imMessage.setFromType(Constant.FROM_TYPE_SYS);
        }
        //判断黑名单（黑名单直接丢弃数据）
        boolean isBlack = isBlack(imMessage);
        if (isBlack) {
            return;
        }
        if (notfriend(imMessage)) {
            return;
        }
        //同一账号发送过来的
        if (messageType != Constant.MESSAGE_TYPR_REDPACKTE && messageType != Constant.MESSAGE_TYPR_GET_REDPACKET&& messageType != Constant.Someone_initiated_the_lucky_red_envelope
                && (imMessage.getDestid() + "").equals(ToolsUtils.getMyUserId())) {
            // Log.i("info","接收小心消息 id= "+imMessage.getFromid()+"destid=="+imMessage.getDestid());
            int msgtype = -1;
            switch (messageType) {
                case Constant.MESSAGE_TYPR_TEXT:
                    msgtype = ChatRecyclerAdapter.TO_USER_MSG;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_IMAGE:
                    msgtype = ChatRecyclerAdapter.TO_USER_IMG;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_FILE:
                    msgtype = ChatRecyclerAdapter.TO_USER_FILE;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_LOCATION:
                    msgtype = ChatRecyclerAdapter.TO_USER_LOCATION;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_VEDIO:
                    msgtype = ChatRecyclerAdapter.TO_USER_VEDIO;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_BQEMOJI:
                    String content = imMessage.getContent();
                    Log.i(TAG, "onMessage: ==========content======" + content);
                    try {
                        MsgEntity receirvemsg = new Gson().fromJson(content, MsgEntity.class);
                        if (receirvemsg.getMsgCodes() != null && !"".equals(receirvemsg.getMsgCodes())) {
                            //大表情
                            msgtype = ChatRecyclerAdapter.TO_USER_EMOJI;
                        } else {
                            //图文混排
                            msgtype = ChatRecyclerAdapter.TO_USER_EMOJI_TEXT;
                        }
                        Log.i(TAG, "onMessage: ==========msgtype======" + msgtype);
                        imMessage.setType(msgtype);
                    } catch (Exception e) {//消息解析失败
                        Log.i(TAG, "解析失败");
                        return;
                    }
                    break;
                case Constant.MESSAGE_TYPR_VOICE:
                    msgtype = ChatRecyclerAdapter.TO_USER_VOICE;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_REDPACKTE:
                    msgtype = ChatRecyclerAdapter.TO_USER_REDPACKET;
                    imMessage.setType(msgtype);
                    break;
                case Constant.Someone_initiated_the_lucky_red_envelope:
                    msgtype = ChatRecyclerAdapter.Send_lucky_Red_Envelopes;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_GET_REDPACKET:
                    msgtype = ChatRecyclerAdapter.TO_USER_REDPACKET;
                    imMessage.setType(msgtype);
                    break;
                case Constant.MESSAGE_TYPR_WITHDRAW:
                    //msgtype = ChatRecyclerAdapter.TO_USER_VOICE;
                    break;

                case Constant.MSG_OF_FREEZE://冻结账号

                    EventBus.getDefault().post(Constant.MSG_OF_FREEZE);
                    App.isManualLogout = true;
                    Intent intent1 = new Intent(mApp, LoginActivity.class);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent1.putExtra("toast", "您的账号涉嫌违规，已经被冻结");
                    ToolsUtils.saveLoginstate(mApp, false, 1);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    //先断掉连接，然后关闭socket
                    try {
                        if (App.getInstance().getSocket() != null) {
                            App.getInstance().getSocket().disconnect();
                            App.getInstance().getSocket().close();
                            App.getInstance().socket = null;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    // Looper.loop();
                    //  ToolsUtils.showToast(mApp,"您的账号在另一台安卓设备登录");
                    mApp.startActivity(intent1);
                    return;

                case Constant.MESSAGE_TYPR_OTHER_LOGIN://其他设备登录同一账号
                    try {
                        GetUUID getUUID = new Gson().fromJson(imMessage.getContent(), GetUUID.class);
                        Log.i("fanhui", "getUUID.getUUID():" + getUUID.getUUID() + "    GetDeviceid.id:" + GetDeviceid.id(ToolsUtils.context));
                        if (null != getUUID) {
//                            if (getUUID.getUUID().equals(ToolsUtils.getMac())){
                            if (getUUID.getUUID().equals(GetDeviceid.id(ToolsUtils.context))) {
                                return;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    EventBus.getDefault().post(Constant.MESSAGE_TYPR_OTHER_LOGIN);
                    App.isManualLogout = true;
                    Intent intent = new Intent(mApp, LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("toast", "您的账号在另一台设备登录");
                    ToolsUtils.saveLoginstate(mApp, false, 1);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    //先断掉连接，然后关闭socket
                    try {
                        if (App.getInstance().getSocket() != null) {
                            App.getInstance().getSocket().disconnect();
                            App.getInstance().getSocket().close();
                            App.getInstance().socket = null;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    // Looper.loop();
                    //  ToolsUtils.showToast(mApp,"您的账号在另一台安卓设备登录");
                    mApp.startActivity(intent);
                    return;
                case Constant.MESSAGE_TYPR_CALL_VIDEO_FROM://视频通话请求

                    break;
                case Constant.MESSAGE_TYPR_CALL_VIDEO_AS://视屏通话应答

                    break;
                case Constant.MESSAGE_TYPR_CALL_VOICE_FROM://语音通话请求

                    break;
                case Constant.MESSAGE_TYPR_CALL_VOICE_AS://语音通话应答

                    break;
                case Constant.MESSAGE_TYPR_GROUP_SET_MANAGER://设置管理员
                    imMessage.setType(ChatGroupRecyclerAdapter.SHOW_SET_MANAGER);
                    //
                    EventBus.getDefault().post(EventBusType.REFRESH_GROUP_USER_MANAGER);
                    break;
                case Constant.MESSAGE_TYPR_GROUP_CANCEL_MANAGER://取消管理员
                    imMessage.setType(ChatGroupRecyclerAdapter.SHOW_CANCEL_MANAGER);
                    //updateRole(imMessage, "3");
                    EventBus.getDefault().post(EventBusType.REFRESH_GROUP_USER_MANAGER);
                    break;
                case Constant.NOTICE_OF_GROUP_OUT:
                    imMessage.setType(ChatGroupRecyclerAdapter.SHOW_BEAN_REMOVE);
                    updateGroupMember(imMessage);
                    break;
                case Constant.MESSAGE_TYPE_BE_JOIN_GROUP:

                    imMessage.setType(ChatGroupRecyclerAdapter.SHOW_BEAN_INVITED_JOIN_GROUP);

                    final String myUserId = ToolsUtils.getMyUserId();
                    final String groupId = String.valueOf(imMessage.getFromid());
                    PGService.getInstance().getGroupById(groupId, myUserId)
                            .subscribe(new AbsAPICallback<ImGroupEntivity>() {
                                @Override
                                public void onNext(ImGroupEntivity groupEntivity) {
                                    groupEntivity.setCurrentid(Long.valueOf(ToolsUtils.getMyUserId()));
                                    groupEntivity.setReceiveTip(1);
                                    groupEntivity.save();
                                    EventBus.getDefault().post(Constant.BLACK_BROADCAST_ADD_GROUP_FOUND_REFRESH);
                                    handleSessionMessage(imMessage);
                                }

                                @Override
                                protected void onResultError(ApiException ex) {
                                    Log.i("-----", "--001ex.getDisplayMessage()--" + ex.getDisplayMessage());
                                }
                            });
                    break;
                default:
                    Log.i(TAG, "onMessage: =====default=====msgtype======" + msgtype);
                    imMessage.setType(msgtype);
                    break;
            }
            imMessage.setSendState(1);
            imMessage.setRead(false);
            if (imMessage.getFromType() == 1) {
                imMessage.setUniqueness(imMessage.getDestid() + "_" + imMessage.getFromid());
            } else if (imMessage.getFromType() == 2) {
                imMessage.setUniqueness(imMessage.getDestid() + "__" + imMessage.getFromid());
            }
            imMessage.save();//直接保存
            ImMessage imMessagebyId = ImMessage.findById(ImMessage.class, imMessage.getId());
            IMMessageToJson.ImMessageToMessageEntivity(imMessagebyId, Constant.FROM_TYPE_SELF);
          /*  if (messageEntivity != null) {
                EventBus.getDefault().post(messageEntivity);
            }*/
            EventBus.getDefault().post(imMessage);
            return;
        }

        //Log.i("info","接收到消息=="+imMessage.toString());
        // boolean isother = (imMessage.getDestid()+ "").equals(ToolsUtils.getMyUserId());//其他设备同一账号的消息
//        Log.i(TAG, "onMessage: ------0001--------别人要求你付款---------");
        String content1 = imMessage.getContent();
        switch (messageType) {
            case Constant.MESSAGE_TYPR_READY: {//ready
                EventBus.getDefault().post(imMessage);
                break;
            }
            case Constant.MESSAGE_TYPR_BQEMOJI: {//文本消息
                MsgEntity receirvemsg = new Gson().fromJson(content1, MsgEntity.class);
                Log.i(TAG, "onMessage: =====222=====receirvemsg======" + receirvemsg);
                if (receirvemsg == null) return;
                if (receirvemsg.getMsgCodes() != null && !"".equals(receirvemsg.getMsgCodes())) {
                    //大表情
                    imMessage.setType(ChatRecyclerAdapter.FROM_USER_EMOJI);
                } else {
                    //图文混排
                    imMessage.setType(ChatRecyclerAdapter.FROM_USER_EMOJI_TEXT);
                    //这里保存有@的数据
                    if (null != receirvemsg.getAiteId() && receirvemsg.getAiteId().size() > 0) {
                        Long aiteId = receirvemsg.getAiteId().get(0);
                        if (("" + aiteId).equals(ToolsUtils.getMyUserId())) {//这里艾特了你
                            AiteEntivity aiteEntivity = new AiteEntivity();
                            aiteEntivity.setMsgId(imMessage.getMsgId());
                            aiteEntivity.setDestId(imMessage.getFromid());
                            aiteEntivity.save();
                            Long id = aiteEntivity.getId();
                            EventBus.getDefault().post("isaiting");
                            //Log.i("info","@存入数据库中的id=="+id);
                        }
                    }
                }
                Log.i(TAG, "onMessage: =====222=====001======");
                // imMessage.setType(ChatRecyclerAdapter.FROM_USER_EMOJI);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                    EventBus.getDefault().post(messageEntivity);
//                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);

                break;
            }
            case Constant.MESSAGE_TYPR_TEXT: {//文本消息
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_MSG);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_IMAGE: {//图片消息
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_IMG);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_LOCATION: {//位置信息
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_LOCATION);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_FILE: {//文件消息
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_FILE);
                String content = imMessage.getContent();
                FileMsgEntivity fileMsgEntivity = new Gson().fromJson(content, FileMsgEntivity.class);
                fileMsgEntivity.setFilished(false);
                String s = new Gson().toJson(fileMsgEntivity);
                imMessage.setContent(s);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_VEDIO: {//小视屏
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_VEDIO);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_FIREND_ONLION: {//好友上线
                break;
            }
            case Constant.MESSAGE_TYPR_FRIEND_OFFLION: {//好友离线
                break;
            }

            case Constant.BLACKLIST_MESSAGE_NOTIFICATION: {//被对方加入黑名单
                Log.i("info", "onMessage: ===74====" + imMessage.toString());
                ImFriendEntivity imFriendEntivity = ImFriendEntivity.findById(ImFriendEntivity.class, imMessage.getDestid());
                if (imFriendEntivity != null) {
                    //修改成单向好友
                    imFriendEntivity.setIsBlack("-1");
                    imFriendEntivity.save();
                    EventBus.getDefault().post(Constant.BLACK_BROADCAST_ADD_GROUP_FOUND_REFRESH);
                }

                break;
            }
            case Constant.CANCEL_BLACKLIST_MESSAGE_NOTIFICATION: {//被对方取消黑名单
                Log.i("info", "onMessage: ===75====" + imMessage.toString());
                ImFriendEntivity imFriendEntivity = ImFriendEntivity.findById(ImFriendEntivity.class, imMessage.getDestid());
                if (imFriendEntivity != null) {
                    //修改成单向好友
                    imFriendEntivity.setIsBlack("0");
                    imFriendEntivity.save();
                    EventBus.getDefault().post(Constant.BLACK_BROADCAST_ADD_GROUP_FOUND_REFRESH);
                }

                break;
            }
            case Constant.MESSAGE_TYPR_BYE_FRIEND: {//解除好友
                //需要删除本地数据库,发送到friendFragment
                //删除数据
                //被对方解除了好友关系，删除本地的好友信息
                ImFriendEntivity imFriendEntivity = ImFriendEntivity.findById(ImFriendEntivity.class, imMessage.getDestid());
                if (imFriendEntivity != null) {
                    //修改成单向好友
                    imFriendEntivity.setIsFriend("0");
                    imFriendEntivity.save();
                    EventBus.getDefault().post(Constant.BLACK_BROADCAST_ADD_GROUP_FOUND_REFRESH);
                }
//                List<MessageEntivity> messageEntivities = MessageEntivity.find(MessageEntivity.class, "fromid=? and destid=? and uniqueness =?",
//                        new String[]{imMessage.getFromid() + "", imMessage.getDestid() + "", "_"});
//                if (messageEntivities.size() > 0) {
//                    messageEntivities.get(0).delete();
//                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
//                }
//
//                MessageEntivity.deleteAll(MessageEntivity.class, "fromid=? and destid=? and uniqueness=?", new String[]{imMessage.getFromid() + "", imMessage.getDestid() + "", "_"});
//                //需要退出当前聊天界面
//                EventBus.getDefault().post(Constant.BYB_FRIEND);
                break;
            }
            case Constant.MESSAGE_TYPR_ADD_GROUP: {//有人加群
                imMessage.setType(ChatGroupRecyclerAdapter.SHOW_PROMP_JOIN_GROUP);
                EventBus.getDefault().post(imMessage);
                // MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                //  EventBus.getDefault().post(messageEntivity);
                imMessage.save();//存储接受的数据
                EventBus.getDefault().post(EventBusType.GROUP_ADD_USER);

//                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
//                imMessage.save();//存储接受的数据
//                if (messageEntivity != null) {
//                    EventBus.getDefault().post(messageEntivity);
//                }else {
//                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
//                }

                break;
            }
            case Constant.MESSAGE_TYPR_BYE_GROUP: {//有人退群
                //删除已经离开群的人
                GroupFriendEntivity.deleteAll(GroupFriendEntivity.class, "belong_group_id=? and uid=?", new String[]{"" + imMessage.getFromid(), "" + imMessage.getDestid()});
                imMessage.setType(ChatGroupRecyclerAdapter.SHOW_PROMP_DELETE_GROUP);
                UserEntivity userEntivity = ToolsUtils.getUser();
                imMessage.setUniqueness(userEntivity.getId() + "__" + imMessage.getFromid());
                imMessage.save();//存储接受的数据
                EventBus.getDefault().post(imMessage);
                // MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                //  EventBus.getDefault().post(messageEntivity);

                //  EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_INVITE_GROUP: {//被邀请加群
                //先去获取群的信息，然后加入本地群
                Log.i("info","走了10这个状态");
                EventBus.getDefault().post(imMessage);//在mainactivity
                break;
            }
            case Constant.MESSAGE_TYPR_VOICE: {//语音
                Log.i("info", "Service++语音消息");
                String json = content1;
                Log.i("info", "json==" + json);
                try {
                    imMessage.setType(ChatRecyclerAdapter.FROM_USER_VOICE);
                    VoiceEntivity voiceEntivity = new Gson().fromJson(json, VoiceEntivity.class);
                    imMessage.setUserVoiceUrl(voiceEntivity.getUrl());
                    imMessage.setUserVoiceTime(voiceEntivity.getTime());
                } catch (Exception e) {
                    imMessage.setType(ChatRecyclerAdapter.FROM_USER_MSG);
                    String content = content1;
                    imMessage.setContent(content);
                }
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_APPLY_FRIEND: {//有人申请加好友
                //Log.i("info", "有人加好友" + imMessage.getFromType());
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);

                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.NOTICE_OF_APPLY_FRIEND: {//有人申请加好友（带一句话）
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);

                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
                break;
            }
            case Constant.MESSAGE_TYPR_APPLE_ADD_FRIEND_AGREE: {//申请加好友被通过
                //申请好友通过后通知到主界面，然后获取好友信息
                EventBus.getDefault().post(imMessage);
                break;
            }
            case Constant.MESSAGE_TYPR_APPLE_GROUP: {//有人申请加入群
                //目前是申请加群后自动同意
                break;
            }
            case Constant.MESSAGE_TYPR_APPLY_GROUP_AGREE: {//申请加入群通过
                //目前是申请加群后自动同意
                break;
            }
            case Constant.MESSAGE_TYPR_OTHER_LOGIN: {//账户从其它地方登陆
                EventBus.getDefault().post(Constant.MAIN_ACTIVITY_TO_LOGIN);
                break;
            }

            case Constant.MSG_OF_FREEZE: {//冻结账号
                EventBus.getDefault().post(Constant.MAIN_ACTIVITY_TO_LOGIN);
                break;
            }

            case Constant.MESSAGE_TYPR_REDPACKTE: {//有红包
                //Log.i("info","收到红包消息了=="+imMessage.toString());
                if (("" + imMessage.getDestid()).equals(ToolsUtils.getMyUserId())) {
                    imMessage.setType(ChatRecyclerAdapter.TO_USER_REDPACKET);
                } else {
                    imMessage.setType(ChatRecyclerAdapter.FROM_USER_REDPACKET);
                }
                imMessage.setReceiveTime(System.currentTimeMillis());
                imMessage.save();//存储接受的数据
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }//
            case Constant.MESSAGE_TYPR_TRANSFRE: {//有人转账给我
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_TRANSFER);
                imMessage.setReceiveTime(System.currentTimeMillis());
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_GET_REDPACKET: {//我发的红包被领取
                if (("" + imMessage.getDestid()).equals(ToolsUtils.getMyUserId())) {
                    imMessage.setType(ChatRecyclerAdapter.TO_USER_REDPACKET);
                } else {
                    imMessage.setType(ChatRecyclerAdapter.FROM_USER_REDPACKET);
                }
                imMessage.setType(ChatRecyclerAdapter.SHOW_PROMP_RED);
                imMessage.setReceiveTime(System.currentTimeMillis());
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_TRANSFRE_OK: {//转账被领取

                imMessage.setType(ChatRecyclerAdapter.FROM_USER_TRANSFER_OTHER_GET);
                imMessage.setReceiveTime(System.currentTimeMillis());
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_TRANSFRE_BACK: {//转账被退回

                imMessage.setType(ChatRecyclerAdapter.SHOW_TRANSFER_BACK);
                imMessage.setReceiveTime(System.currentTimeMillis());
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_REDPACKET_BACK: {//红包被退回

                imMessage.setType(ChatRecyclerAdapter.SHOW_REDPACKET_BACK);
                imMessage.setReceiveTime(System.currentTimeMillis());
                EventBus.getDefault().post(imMessage);

                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_FRIEND_ADD: {//默认加好友成功！广播到friendFragment界面
                EventBus.getDefault().post(imMessage);
                break;
            }
            case Constant.MESSAGE_TYPR_REDPACKET_NOFINISH: {//
                imMessage.setType(ChatGroupRecyclerAdapter.SHOW_PROMP_REDPACKET_NOFINISH);
                imMessage.setReceiveTime(System.currentTimeMillis());
                EventBus.getDefault().post(imMessage);

                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_GROUP_CHANGE: {// 群名称被修改
                EventBus.getDefault().post(imMessage);//通知到群列表
                break;
            }
            case Constant.MESSAGE_TYPR_GROUP_CHANGE_UNAME: {// 群成员昵称有修改
                EventBus.getDefault().post(imMessage);//通知到群聊天，这个目前只是在群聊天更新，因为每次进入聊天都会查询群成员
                break;
            }
            case Constant.MESSAGE_TYPR_WITHDRAW: {//消息被撤回
                imMessage.setType(ChatRecyclerAdapter.FROM_WITHDRAW_MSG);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                Log.i(TAG, "ServiceDataAnalyse: -------这里是接收的消息的撤回，走不走？------imMessage=" + imMessage.toString());
                WithdrawEntity wd = new Gson().fromJson(imMessage.getContent(), WithdrawEntity.class);
                List<ImMessage> list = ImMessage.find(ImMessage.class, "msg_id=?", new String[]{wd.getMsgid()});
                if (null != list && list.size() > 0) {
                    list.get(0).delete();
                }
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
                EventBus.getDefault().post(imMessage);
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_READ://消息已读
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_MSG_READ);
                ReadedEntity readedEntity = new ReadedEntity();
                JSONArray jsonArray = null;
                try {
                    jsonArray = new JSONArray(imMessage.getContent());
                    if (jsonArray.length() > 0) {
                        JSONObject jsobj = jsonArray.getJSONObject(0);
                        final String msgid = jsobj.getString("id");
                        readedEntity.setMsgid(msgid);
                        readedEntity.setUniqueness(imMessage.getUniqueness());
                        readedEntity.save();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //这里暂不存储消息
                EventBus.getDefault().post(imMessage);
                break;
            case Constant.MESSAGE_TYPR_CIRCLE:// 有朋友发了朋友圈
            case Constant.MESSAGE_TYPR_CIRCLE_COMMON: //评论
            case Constant.MESSAGE_TYPR_CIRCLE_PRASIE: //赞
                CircleNums circleNums = IMMessageToJson.ImMessageToCircleNum(imMessage);
                EventBus.getDefault().post(circleNums);
                /*ToolsUtils.saveCirclenums(App.getInstance().getApplicationContext(),true);
                EventBus.getDefault().post(new CircleNums("1"));*/
                break;
            case Constant.MESSAGE_TYPR_TRANSFOR_TOPAY: {//要求你付款
                Log.i(TAG, "onMessage: --------------别人要求你付款---------");
                Intent intent = new Intent(mApp, PayActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("topay", imMessage);
                mApp.startActivity(intent);
                //EventBus.getDefault().post(imMessage);
                break;
            }
            case Constant.MESSAGE_TYPR_AAMONEY_GET: {//有人发aa收款
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_AA);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }

            case Constant.Someone_initiated_the_lucky_red_envelope: {//有人发幸运红包

                //Log.i("info","收到红包消息了=="+imMessage.toString());
                if (("" + imMessage.getDestid()).equals(ToolsUtils.getMyUserId())) {
                    Log.i("info","群id"+imMessage.getDestid());
                    Log.i("info","群id"+ToolsUtils.getMyUserId());
                    imMessage.setType(ChatRecyclerAdapter.Send_lucky_Red_Envelopes);
                } else {
                    imMessage.setType(ChatRecyclerAdapter.Lucky_Red_Envelope);
                }
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                Log.i("info","发幸运红包消息"+messageEntivity);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }

            case Constant.MESSAGE_TYPR_TRANSFOR_ISCOME: {//付款到账通知
                //imMessage.setType(ChatRecyclerAdapter.FROM_USER_PAYFOR);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                    Log.i("info","收到了收款到账的消息"+messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_READ_DELETE: {//阅后即焚
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_READDELETE);
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                imMessage.save();//存储接受的数据
                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
//                EventBus.getDefault().post(messageEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                break;
            }
            case Constant.MESSAGE_TYPR_CALL_VIDEO_FROM: {//视屏通话请求
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_CALLVIDEO);
                imMessage.save();
                if (App.iscallvideoing) {
                    Intent intent2 = new Intent("easy.message");
                    intent2.putExtra("immessage", imMessage);
                    App.getInstance().sendBroadcast(intent2);
                    if (imMessage.getContent().equals("0")) {
                        EventBus.getDefault().post(imMessage);//取消视屏请求
                    }
                } else {
                    if (imMessage.getContent().equals("1")) {
                        Intent intent = new Intent(mApp, AScallActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("userid", ToolsUtils.getMyUserId());
                        intent.putExtra("destid", imMessage.getDestid() + "");
                        intent.putExtra("type", "1");
                        intent.putExtra("name", imMessage.getFromname());
                        intent.putExtra("headurl", imMessage.getImageIconUrl());
                        App.rtcdestid = imMessage.getDestid() + "";
                        mApp.startActivity(intent);
                    }
                }
            }
            break;
            case Constant.MESSAGE_TYPR_CALL_VIDEO_AS: {// 视屏通话应答
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_CALLVIDEO_AS);
                imMessage.save();
                //EventBus.getDefault().post(imMessage.getContent());
                Intent intent = new Intent("easy.message");
                intent.putExtra("immessage", imMessage);
                EventBus.getDefault().post(imMessage);
            }
            break;
            case Constant.MESSAGE_TYPR_CANCLE_CALLVIDEO: {//断开视屏通话
                Intent intent = new Intent("easy.message");
                intent.putExtra("immessage", imMessage);
                App.getInstance().sendBroadcast(intent);
                break;
            }
            case Constant.MESSAGE_TYPR_CALL_OFFER: {

                imMessage.save();
                EventBus.getDefault().post(imMessage);

                /*Log.i("info", "onMessage: ===47");
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_CALLVIDEO);
                imMessage.save();
                if (App.iscallvideoing) {
                    Intent intent2 = new Intent("easy.message");
                    intent2.putExtra("immessage", imMessage);
                    App.getInstance().sendBroadcast(intent2);
                    if (imMessage.getContent().equals("0") && App.iscallvideoing) {
                        EventBus.getDefault().post(imMessage);//取消视屏请求
                    }
                } else {
                   // if (imMessage.getContent().equals("1") && !App.iscallvideoing) {
                        Intent intent = new Intent(mApp, CalldideoActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("userid", ToolsUtils.getMyUserId());
                        intent.putExtra("destid", imMessage.getDestid() + "");
                        intent.putExtra("type", "1");
                        intent.putExtra("name", imMessage.getFromname());
                        intent.putExtra("headurl", imMessage.getImageIconUrl());
                        intent.putExtra("immessage",imMessage);
                        mApp.startActivity(intent);
                  //  }
                }*/
                /*JSONObject contentjs = null;
                try {
                    contentjs = new JSONObject(imMessage.getContent());
                    SessionDescription sdp = new SessionDescription(
                            SessionDescription.Type.fromCanonicalForm(contentjs.getString("type")),
                            contentjs.getString("sdp"));
                    pc.setRemoteDescription(observer, sdp);
                    EventBus.getDefault().post(imMessage);
                   *//* pc.createAnswer(observer, RtcUtil.getPcConstraints());
                    //发送answer消息
                    JSONObject payload = new JSONObject();
                    payload.put("type", sdp.type.canonicalForm());
                    payload.put("sdp", sdp.description);
                    String sendSdp= payload.toString();
                    socket.emit("chat", IMMessage.createSimpleMsg(sendSdp, 48), new Ack() {
                        @Override
                        public void call(Object... args) {
                            System.out.println("answer sent...");
                        }
                    });*//*
                } catch (JSONException e) {
                    e.printStackTrace();
                }*/

            }
            break;
            case Constant.MESSAGE_TYPR_CALL_ANSWER: {
                NoticeBean noticeBean = new Gson().fromJson(imMessage.getContent(), NoticeBean.class);
                int issave = 0;
                List<ImMessage> imMessageS1 = ImMessage.find(ImMessage.class, "fromid=? and message_type=?", new String[]{"" + imMessage.getFromid(), "" + Constant.MESSAGE_TYPR_CALL_OFFER});
                if (null != imMessageS1 && imMessageS1.size() > 0) {
                    for (int i = 0; i < imMessageS1.size(); i++) {
                        ImMessage mImMessage1 = imMessageS1.get(i);
                        try {
                            String content = "";
                            if (mImMessage1.getContent().length() > 2) {
                                content = mImMessage1.getContent().substring(1, mImMessage1.getContent().length() - 1);
                            }
                            NoticeBean noticeBean1 = new Gson().fromJson(content, NoticeBean.class);
                            if (null != noticeBean && null != noticeBean1 && noticeBean.getId().equals(noticeBean1.getId())) {
                                mImMessage1.setContent("\"" + new Gson().toJson(noticeBean) + "\"");
                                mImMessage1.setPayed(false);
                                mImMessage1.setSendTime(System.currentTimeMillis());
                                mImMessage1.setMessageType(Constant.MESSAGE_TYPR_CALL_ANSWER);
                                mImMessage1.save();
                                issave++;
                                continue;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (issave <= 0) {
                    List<ImMessage> imMessageS2 = ImMessage.find(ImMessage.class, "fromid=? and message_type=?", new String[]{"" + imMessage.getFromid(), "" + Constant.MESSAGE_TYPR_CALL_OFFER});
                    if (null != imMessageS2 && imMessageS2.size() > 0) {
                        for (int i = 0; i < imMessageS2.size(); i++) {
                            ImMessage mImMessage2 = imMessageS2.get(i);
                            try {
                                String content = "";
                                if (mImMessage2.getContent().length() > 2) {
                                    content = mImMessage2.getContent().substring(1, mImMessage2.getContent().length() - 1);
                                }
                                NoticeBean noticeBean2 = new Gson().fromJson(content, NoticeBean.class);
                                if (null != noticeBean && null != noticeBean2 && noticeBean.getId().equals(noticeBean2.getId())) {
                                    mImMessage2.setContent("\"" + new Gson().toJson(noticeBean) + "\"");
                                    mImMessage2.setSendTime(System.currentTimeMillis());
                                    mImMessage2.setPayed(false);
                                    mImMessage2.save();
                                    issave++;
                                    continue;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }

                Log.i("wgdinfo", "onMessage: ===48------012----");
                if (issave <= 0) {
                    Log.i("wgdinfo", "onMessage: ===48------013----");
                    imMessage.setPayed(false);
                    imMessage.save();
                }
                EventBus.getDefault().post(imMessage);
                //EventBus.getDefault().post(imMessage);
               /* JSONObject contentjs = null;
                try {
                    contentjs = new JSONObject(imMessage.getContent());
                    SessionDescription sdp = new SessionDescription(
                            SessionDescription.Type.fromCanonicalForm(contentjs.getString("type")),
                            contentjs.getString("sdp"));
                    pc.setRemoteDescription(observer,sdp);
                } catch (JSONException e) {
                    e.printStackTrace();
                }*/
            }
            break;
            case Constant.MESSAGE_TYPR_CALL_ICE:
                Log.i("info", "onMessage: ===49====" + imMessage.toString());
                CircleNums circleNums49 = IMMessageToJson.ImMessageToCircleNum(imMessage);
                EventBus.getDefault().post(circleNums49);
                // EventBus.getDefault().post(imMessage);
               /* JSONObject contentjs= null;
                try {
                    contentjs = new JSONObject(imMessage.getContent());
                    IceCandidate candidate = new IceCandidate(
                            contentjs.getString("sdpMid"),
                            contentjs.getInt("sdpMLineIndex"),
                            contentjs.getString("candidate")
                    );
                    App.pc.addIceCandidate(candidate);
                } catch (JSONException e) {
                    e.printStackTrace();
                }*/
                break;
            case Constant.MESSAGE_TYPR_CHANGE_GROUP_HEAD:
                Log.i("info", "onMessage: ===46====" + imMessage.toString());

                ImGroupEntivity imGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, imMessage.getFromid());
                if (null != imGroupEntivity) {
                    Log.i("info", "onMessage: ===46==01==");
                    imGroupEntivity.setHeadUrl(imMessage.getContent());
                    imGroupEntivity.save();
                }
                Log.i("info", "onMessage: ===46==02==");
//                List<ImGroupEntivity> imGroupEntivitys = ImGroupEntivity.find(ImGroupEntivity.class, "creater_id=?", new String[]{""+imMessage.getFromid()});
//                if (null!=imGroupEntivitys && imGroupEntivitys.size() > 0){
//                    ImGroupEntivity imGroupEntivity = imGroupEntivitys.get(0);
//                    imGroupEntivity.setHeadUrl(imMessage.getContent());
//                    imGroupEntivity.save();
//                }
                EventBus.getDefault().post(new EventBusTypeObject(EventBusType.REFRESH_LIST_GROUP_HEAD, "" + imMessage.getFromid(), imMessage.getContent()));
                break;
            case Constant.MESSAGE_TYPR_FRIEND_HEAD:
                Log.i("info", "onMessage: ===28====" + imMessage.toString());
                imMessage.setType(ChatRecyclerAdapter.FROM_USER_FRIEND_MP);
                imMessage.save();
                EventBus.getDefault().post(imMessage);
                MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);

                if (messageEntivity != null) {
                    EventBus.getDefault().post(messageEntivity);
                } else {
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                }
                break;
            case Constant.MESSAGE_TYPR_GROUP_SET_MANAGER:
                Log.i("info", "onMessage: ===55====" + imMessage.toString());

                imMessage.setType(ChatGroupRecyclerAdapter.SHOW_SET_MANAGER);
                imMessage.save();
                EventBus.getDefault().post(imMessage);
                //(imMessage, "2");
                EventBus.getDefault().post(EventBusType.REFRESH_GROUP_USER_MANAGER);
                break;
            case Constant.MESSAGE_TYPR_GROUP_CANCEL_MANAGER:
                Log.i("info", "onMessage: ===57====" + imMessage.toString());

                imMessage.setType(ChatGroupRecyclerAdapter.SHOW_CANCEL_MANAGER);
                imMessage.save();
                EventBus.getDefault().post(imMessage);
                //updateRole(imMessage, "3");
                EventBus.getDefault().post(EventBusType.REFRESH_GROUP_USER_MANAGER);
                break;
            case Constant.MESSAGE_TYPR_GROUP_BRIFE:
                Log.i("info", "onMessage: ===56====" + imMessage.toString());
                try {
                    ImGroupEntivity mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, imMessage.getFromid());
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    mImGroupEntivity.setDescriptions(json.getString("descriptions"));
                    mImGroupEntivity.save();
                    EventBus.getDefault().post(EventBusType.REFRESH_GROUP_BRIFE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constant.MESSAGE_TYPR_GROUP_DISMISS:
                Log.i("info", "onMessage: ===51====" + imMessage.toString());
                try {

                    /*{id=null, version=0, devType=null, geoId=0, msgId=8def89fb-059c-4e81-b98d-fa1003dbbeb8,
                     fromid=2373, fromname=123023ab, fromType=3, destid=17231, content={"groupName":"试试名字，023","destId":17231,
                     "groupId":2373}, messageType=51, sendTime=1505376980744, receiveTime=0, status=0, userVoiceTime=0.0,
                      userVoicePath={"groupName":"试试名字，023","destId":17231,"groupId":2373}, userVoiceUrl=null,
                      fileUrl={"groupName":"试试名字，023","destId":17231,"groupId":2373}, filePath=null,
                       imageIconUrl=http://123.57.47.19:8080/wmsMobile/upload/1503453465372_PiMNwl150345346398517231.png,
                       uniqueness=null, type=0, sendState=0, content_fy=null, isShowFY=, isRead=false, isPayed=false, isklopen=false}*/

                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String name = json.getString("groupName");
                    String destId = json.getString("destId");
                    String groupId = json.getString("groupId");


                    //清除本地群
                    List<ImGroupEntivity> groupEntivities = ImGroupEntivity.find(ImGroupEntivity.class, "currentid=?", "" + ToolsUtils.getMyUserId());
                    for (int i = 0; i < groupEntivities.size(); i++) {
                        ImGroupEntivity groupEntivity = groupEntivities.get(i);
                        if ((groupEntivity.getId() + "").equals("" + imMessage.getFromid())) {
                            groupEntivity.delete();
                            break;
                        }
                    }
                    //清除本地群的聊天信息
                    List<MessageEntivity> messageEntivities = MessageEntivity.find(MessageEntivity.class, "fromid=? and destid=? and uniqueness=?", new String[]{ToolsUtils.getMyUserId() + "", "" + imMessage.getFromid(), "__"});
                    if (messageEntivities.size() > 0) {
                        MessageEntivity.delete(messageEntivities.get(0));
                    }

                    //清楚本地群相关的通知
                    List<MessageEntivity> messageEntivities2 = MessageEntivity.find(MessageEntivity.class, "fromid = ? and type = ? "
                            , new String[]{ToolsUtils.getMyUserId() + "", Constant.MESSAGE_TYPR_GROUP_TRANS_ME + ""});
                    if (null != messageEntivities2 && messageEntivities2.size() > 0) {
                        for (int i = 0; i < messageEntivities2.size(); i++) {
                            MessageEntivity messageEntivity1 = messageEntivities2.get(i);
                            if (groupId.equals("" + messageEntivity1.getDestid())) {
                                messageEntivity1.delete();
                            }
                        }
                    }

                    EventBus.getDefault().post(Constant.BLACK_BROADCAST_ADD_GROUP_FOUND_REFRESH);
                    EventBus.getDefault().post(EventBusType.GROUP_TRANS_DELETE_GRPOUP);
                    EventBus.getDefault().post(new EventBusTypeObject(EventBusType.SHOW_MAIN_TOAST, groupId, "群组\"" + name + "\"被群主解散！"));
                    /** 退出群组不是常用的通知，直接刷新界面也没什么的 */
                    EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constant.MESSAGE_TYPR_GROUP_TRANS_ME:
                Log.i("info", "onMessage: ===52====" + imMessage.toString());
                /*{id=null, version=0, devType=null, geoId=0, msgId=286b4487-a9a2-4a9f-9e8e-e7bf5ca368d0,
                fromid=2402, fromname=123025, fromType=3, destid=17244,
                content={"groupName":"测试转让02","destId":17244,"groupId":2402},
                messageType=52, sendTime=1505379656791, receiveTime=0, status=0, userVoiceTime=0.0,
                 userVoicePath={"groupName":"测试转让02","destId":17244,"groupId":2402}, userVoiceUrl=null,
                 fileUrl={"groupName":"测试转让02","destId":17244,"groupId":2402}, filePath=null,
                 imageIconUrl=http://123.57.47.19:8080/wmsMobile/upload/1503362239394_Wl31cR150336223643917244.png,
                 uniqueness=null, type=0, sendState=0, content_fy=null, isShowFY=, isRead=false, isPayed=false, isklopen=false}*/
                try {
                    MessageEntivity messageEntivity2 = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                   System.out.print("sss"+messageEntivity2);
                    if (messageEntivity2 != null) {
                        EventBus.getDefault().post(messageEntivity2);
                    } else {
                        EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case Constant.MESSAGE_TYPR_GROUP_TRANS_AGREE:
                Log.i("info", "onMessage: ===53====" + imMessage.toString());
                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String name = json.getString("groupName");
                    String destId = json.getString("destId");
                    String groupId = json.getString("groupId");
                    String destName = destId;
                    List<GroupFriendEntivity> groupFriendEntivities1 = GroupFriendEntivity.find(GroupFriendEntivity.class, "belong_group_id=? and uid=?",
                            new String[]{groupId, destId});

                    if (null != groupFriendEntivities1 && groupFriendEntivities1.size() > 0) {
                        destName = groupFriendEntivities1.get(0).getMarkName();
                        if (null == destName || "".equals(destName)) {
                            destName = groupFriendEntivities1.get(0).getName();
                        }
                        if (null == destName || "".equals(destName)) {
                            destName = destId;
                        }
                    }

                    ImGroupEntivity mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, Long.parseLong(groupId));
                    mImGroupEntivity.setCreaterId(Long.parseLong(destId));
                    mImGroupEntivity.save();

                    List<GroupFriendEntivity> groupFriendEntivitiesY = GroupFriendEntivity.find(GroupFriendEntivity.class, "uid = ? and belong_group_id=?",
                            new String[]{destId, groupId});
                    if (null != groupFriendEntivitiesY && groupFriendEntivitiesY.size() > 0) {
                        GroupFriendEntivity mmm = groupFriendEntivitiesY.get(0);
                        mmm.setRole("1");
                        mmm.save();
                    }
                    List<GroupFriendEntivity> groupFriendEntivities = GroupFriendEntivity.find(GroupFriendEntivity.class, "uid = ? and belong_group_id=?",
                            new String[]{ToolsUtils.getMyUserId(), groupId});
                    if (null != groupFriendEntivities && groupFriendEntivities.size() > 0) {
                        GroupFriendEntivity mmm = groupFriendEntivities.get(0);
                        mmm.setRole("3");
                        mmm.save();
                    }

                    EventBus.getDefault().post(new EventBusTypeObject(EventBusType.SHOW_MAIN_TOAST, "", "你转让群组\"" + name + "\"被\"" + destName + "\"同意！"));
                    EventBus.getDefault().post(new EventBusTypeObject(EventBusType.GROUP_TRANS_AGREE, groupId, ""));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constant.MESSAGE_TYPR_GROUP_TRANS_UNAGREE:
                Log.i("info", "onMessage: ===54====" + imMessage.toString());
                try {

                    /*{id=null, version=0, devType=null, geoId=0, msgId=ebb9bd3d-bb72-4ff2-a35e-b2fa2b834a6c,
                    fromid=2423, fromname=123025, fromType=3, destid=17244,
                     content={"groupName":"擦擦汗","destId":17244,"groupId":2423},
                     messageType=54, sendTime=1505446059003, receiveTime=0, status=0, userVoiceTime=0.0,
                     userVoicePath={"groupName":"擦擦汗","destId":17244,"groupId":2423}, userVoiceUrl=null,
                     fileUrl={"groupName":"擦擦汗","destId":17244,"groupId":2423}, filePath=null,
                     imageIconUrl=http://123.57.47.19:8080/wmsMobile/upload/1503362239394_Wl31cR150336223643917244.png,
                     uniqueness=null, type=0, sendState=0, content_fy=null, isShowFY=, isRead=false, isPayed=false, isklopen=false}*/

                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String name = json.getString("groupName");
                    String destId = json.getString("destId");
                    String groupId = json.getString("groupId");
                    String destName = destId;

                    List<GroupFriendEntivity> groupFriendEntivities1 = GroupFriendEntivity.find(GroupFriendEntivity.class, "belong_group_id=? and uid=?",
                            new String[]{groupId, destId});

                    if (null != groupFriendEntivities1 && groupFriendEntivities1.size() > 0) {
                        destName = groupFriendEntivities1.get(0).getMarkName();
                        if (null == destName || "".equals(destName)) {
                            destName = groupFriendEntivities1.get(0).getName();
                        }
                        if (null == destName || "".equals(destName)) {
                            destName = destId;
                        }
                    }

                    EventBus.getDefault().post(new EventBusTypeObject(EventBusType.SHOW_MAIN_TOAST, "", "你转让群组\"" + name + "\"被\"" + destName + "\"拒绝！"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constant.MESSAGE_GROUP_ADD_APPLY:
                Log.i("info", "onMessage: ===59====" + imMessage.toString());
                try {

                    MessageEntivity messageEntivity2 = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
                    if (messageEntivity2 != null) {
                        EventBus.getDefault().post(messageEntivity2);
                    } else {
                        EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
                    }

//                    EventBus.getDefault().post(new EventBusTypeObject(EventBusType.SHOW_MAIN_TOAST, "", "你转让群组被拒绝！"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constant.NOTICE_OF_GROUP_REQUEST_YES:// 同意加群(我的申请被同意)
                Log.i("info", "onMessage: ===60====" + imMessage.toString());

                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String adminName = json.getString("adminName");
                    String groupName = json.getString("groupName");
                    String groupId = json.getString("groupId");
                    String adminId = json.getString("adminId");

//                    if (null==groupName|| TextUtils.isEmpty(groupName))groupName = groupId;
                    EventBus.getDefault().post(new EventBusTypeData(EventBusType.GROUP_APPLY_GRPOUP_REFRENSH_Y, groupId));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case Constant.NOTICE_OF_GROUP_REQUEST_NO:// 拒绝加群(我的申请被拒绝)
                Log.i("info", "onMessage: ===61====" + imMessage.toString());

                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String adminName = json.getString("adminName");
                    String groupName = json.getString("groupName");
                    String groupId = json.getString("groupId");
                    String adminId = json.getString("adminId");

                    if (null == groupName || TextUtils.isEmpty(groupName)) groupName = groupId;
                    EventBus.getDefault().post(new EventBusTypeData(EventBusType.GROUP_APPLY_GRPOUP_REFRENSH_N, "您加入群" + groupName + "的申请被拒绝"));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case Constant.NOTICE_whole_OF_GROUP_SILENCE_NO://全体取消禁言

                Log.i("info", "onMessage: ===73====" + imMessage.toString());

                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String groupId = json.getString("groupId");

                    ImGroupEntivity mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, Long.parseLong(groupId));

                    mImGroupEntivity.setSilenceP("0");
                    mImGroupEntivity.save();
                    ImGroupEntivity mImGroupEntivity1 = ImGroupEntivity.findById(ImGroupEntivity.class, Long.parseLong(groupId));
                    System.out.print("11"+mImGroupEntivity1);
                    EventBus.getDefault().post(EventBusType.GROUP_NOSPICK_GRPOUP_REFRENSH);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case Constant.NOTICE_whole_OF_GROUP_SILENCE_YES:// 全体禁言成功（我被禁言）
                Log.i("info", "onMessage: ===72====" + imMessage.toString());

                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String groupId = json.getString("groupId");

                    ImGroupEntivity mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, Long.parseLong(groupId));

                        mImGroupEntivity.setSilenceP("1");
                        mImGroupEntivity.save();

                    EventBus.getDefault().post(EventBusType.GROUP_NOSPICK_GRPOUP_REFRENSH);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

/*



                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String groupId = json.getString("groupId");

                    //TODO 这里要加通知，在群聊天页面接收
                    EventBus.getDefault().post(EventBusType.GROUP_NOSPICK_GRPOUP_REFRENSH);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;*/

            case Constant.NOTICE_OF_GROUP_SILENCE_YES:// 禁言成功（我被禁言）
                Log.i("info", "onMessage: ===62====" + imMessage.toString());
                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String adminName = json.getString("adminName");
                    String groupName = json.getString("groupName");
                    String groupId = json.getString("groupId");
                    String adminId = json.getString("adminId");
                    String silenceTime = json.getString("silenceTime");
                    List<GroupFriendEntivity> groupFriendEntivities = GroupFriendEntivity.find(GroupFriendEntivity.class, "belong_group_id=? and uid=?", new String[]{groupId, ToolsUtils.getMyUserId()});
                    if (null != groupFriendEntivities && groupFriendEntivities.size() > 0) {
                        GroupFriendEntivity groupFriendEntivity = groupFriendEntivities.get(0);
                        groupFriendEntivity.setSilence(silenceTime);
                        groupFriendEntivity.save();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            case Constant.NOTICE_OF_GROUP_SILENCE_NO:// 取消禁言（我被取消禁言）
                Log.i("info", "onMessage: ===62====" + imMessage.toString());
                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String adminName = json.getString("adminName");
                    String groupName = json.getString("groupName");
                    String groupId = json.getString("groupId");
                    String adminId = json.getString("adminId");
                    String silenceTime = json.getString("silenceTime");
                    List<GroupFriendEntivity> groupFriendEntivities = GroupFriendEntivity.find(GroupFriendEntivity.class, "belong_group_id=? and uid=?", new String[]{groupId, ToolsUtils.getMyUserId()});

                    if (null != groupFriendEntivities && groupFriendEntivities.size() > 0) {
                        GroupFriendEntivity groupFriendEntivity = groupFriendEntivities.get(0);
                        groupFriendEntivity.setSilence(silenceTime);
                        groupFriendEntivity.save();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //TODO 这里要加通知，在群聊天页面接收
                EventBus.getDefault().post(EventBusType.GROUP_NOSPICK_GRPOUP_REFRENSH);

//                break;
//                Log.i("info", "onMessage: ===63===="+ imMessage.toString());
                break;

            case Constant.NOTICE_OF_GROUP_LEVEL_NO://
                Log.i("info", "onMessage: ===58====" + imMessage.toString());
                try {
                    com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
                    String groupId = json.getString("groupId");
                    String expire = json.getString("expire");
                    ImGroupEntivity mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, Long.parseLong(groupId));
                    mImGroupEntivity.setExpire(expire);
                    mImGroupEntivity.save();
//GROUP_APPLY_GRPOUP_LEVEL_N
                    EventBus.getDefault().post(EventBusType.GROUP_APPLY_GRPOUP_LEVEL_N);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constant.NOTICE_OF_GROUP_OUT://
                Log.i("info", "onMessage: ===64====" + imMessage.toString());

                imMessage.setType(ChatGroupRecyclerAdapter.SHOW_BEAN_REMOVE);
                imMessage.save();
                EventBus.getDefault().post(imMessage);
                updateGroupMember(imMessage);
                break;

            case Constant.MESSAGE_TYPE_INVATE_JOIN_GROUP:

                imMessage.setType(ChatRecyclerAdapter.FROM_USER_INVITE_JOIN_GROUP);
                EventBus.getDefault().post(imMessage);
                imMessage.save();//存储接受的数据
                handleSessionMessage(imMessage);
                break;
            case Constant.NOTICE_OF_MONEY_ADD_FOR_JOIN_GROUP:

                EventBus.getDefault().post(imMessage);
                imMessage.save();//存储接受的数据
                handleSessionMessage(imMessage);
                break;
            case Constant.NOTICE_OF_MONEY_REDUCE_JOIN_GROUP:

                EventBus.getDefault().post(imMessage);
                imMessage.save();//存储接受的数据
                handleSessionMessage(imMessage);
                break;
            default:
                break;
        }

    }

    private static void handleSessionMessage(ImMessage imMessage) {
        MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_OTHER);
        if (messageEntivity != null) {
            EventBus.getDefault().post(messageEntivity);
        } else {
            EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
        }
    }

    private static void updateGroupMember(ImMessage imMessage) {

        com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
        String name = json.getString("name");
        Long groupId = imMessage.getFromid();
        List<GroupFriendEntivity> groupFriendEntivities = GroupFriendEntivity.find(GroupFriendEntivity.class,
                "belong_group_id=? and name=?", String.valueOf(groupId), name);
        if (groupFriendEntivities != null && groupFriendEntivities.size() > 0) {
            GroupFriendEntivity groupFriendEntivity = groupFriendEntivities.get(0);
            long uid = groupFriendEntivity.getUid();
            if (String.valueOf(uid).equals(ToolsUtils.getMyUserId())) {
                EventBus.getDefault().post(new EventBusTypeData(EventBusType.GROUP_APPLY_GRPOUP_OUT, String.valueOf(groupId)));
            }
            groupFriendEntivity.delete();
            EventBus.getDefault().post(new EventBusTypeData(EventBusType.GROUP_APPLY_GRPOUP_OUT_OTH, String.valueOf(groupId), String.valueOf(uid)));
        }
    }

    private static String getRunningActivityName(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String runningActivity = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
        return runningActivity;
    }

    /**
     * 查看消息是否是发给黑名单
     *
     * @param imMessage
     * @return
     */
    private static boolean isBlack(ImMessage imMessage) {
        List<ImFriendEntivity> imFriendEntivities = ImFriendEntivity.find(ImFriendEntivity.class, "currentid=? and is_black=?", imMessage.getFromid() + "", Constant.IS_BLACK);
        for (int i = 0; i < imFriendEntivities.size(); i++) {
            ImFriendEntivity imFriendEntivity = imFriendEntivities.get(i);
            if ((imFriendEntivity.getId() + "").equals(imMessage.getDestid() + "")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 查看消息是否是非好友发来的
     * 非好友消息
     * 不是好友的消息不处理
     * <p>
     * 有些通知的类型是单聊，是非好友发过来的，但是需要处理
     * 比如：别人通知我付款
     *
     * @param imMessage
     * @return
     */
    private static boolean notfriend(ImMessage imMessage) {
        if (Constant.FROM_TYPE_MAM == imMessage.getFromType()) {
            List<ImFriendEntivity> imFriendEntivities = ImFriendEntivity.find(ImFriendEntivity.class, "currentid=? and id=?", imMessage.getFromid() + "", "" + imMessage.getDestid());
            if (null == imFriendEntivities || imFriendEntivities.size() <= 0
//                || "17192".equals(""+imMessage.getDestid())
                    ) {
                Integer messageType = imMessage.getMessageType();
                switch (messageType) {
                    case Constant.MESSAGE_TYPR_TRANSFOR_TOPAY:
                        //别人通知我付款
                        return false;
                }
                return true;
            }
            return false;
        } else {
            return false;
        }

    }


    private static void updateRole(ImMessage imMessage, String role) {

        MessageEntivity messageEntivity = IMMessageToJson.ImMessageToMessageEntivity(imMessage, Constant.FROM_TYPE_SYS);
        if (messageEntivity != null) {
            EventBus.getDefault().post(messageEntivity);
        } else {
            EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
        }

        try {
            ImGroupEntivity mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, imMessage.getFromid());
            com.alibaba.fastjson.JSONObject json = JSON.parseObject(imMessage.getContent());
            String name = json.getString("name");
            List<GroupFriendEntivity> groupFriendEntivities3 = GroupFriendEntivity.find(GroupFriendEntivity.class,
                    "belong_group_id=? and name=?", new String[]{"" + mImGroupEntivity.getId(), name});
//                    Log.i("info", "onMessage: ===55==01==");
            if (null != groupFriendEntivities3 && groupFriendEntivities3.size() > 0) {
                GroupFriendEntivity mGroupFriendEntivity = groupFriendEntivities3.get(0);
//                            Log.i("info", "onMessage: ===55==03==mGroupFriendEntivity.getUid()="+mGroupFriendEntivity.getUid());
                mGroupFriendEntivity.setRole(role);
                mGroupFriendEntivity.save();
            }
//                    EventBus.getDefault().post(new EventBusTypeObject(EventBusType.REFRESH_GROUP_USER_MANAGER, name, role));
            EventBus.getDefault().post(EventBusType.REFRESH_GROUP_USER_MANAGER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
